package com.zhiying.zfb;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBManager {

	private SQLiteHelper dbHelper;
	
	public DBManager (Context context) {
		dbHelper = new SQLiteHelper(context);
	}
	
	public int insert(ZfbInfo zfbInfo) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("insert into tab_zfb values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",new Object[]{
					null,
					zfbInfo.riqi,
					zfbInfo.benjin,
					zfbInfo.shengyutianshu,
					zfbInfo.yuqililv,
					zfbInfo.bianxianlilv,
					zfbInfo.kebianxian,
					zfbInfo.zongtianshu,
					zfbInfo.gudinglilv,
					zfbInfo.bianxianshouxufei,
					zfbInfo.yingfulixi,
					zfbInfo.shijidaozhangjine,
					zfbInfo.bianxianhoushouyi,
					zfbInfo.bubianxianshouyi,
					zfbInfo.jinglirun
			});
			db.setTransactionSuccessful(); 
		} catch(Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}
	
	public int delete(ZfbInfo zfbInfo) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("delete from tab_zfb where id=?",new Object[]{
					zfbInfo.id
					});
			db.setTransactionSuccessful(); 
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		return 1;
	}
	
	public float sum(String sumid) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select sum(" + sumid + ") from tab_zfb",null);
		float textfloat = 0;
		if(cursor.moveToNext()) {
			textfloat = cursor.getFloat(0);
		}
		db.close();
		cursor.close();
		return textfloat;
	}
	
	public void query(List<ZfbInfo> list) {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		ZfbInfo zfbInfo;
		cursor = db.rawQuery("select * from tab_zfb",null);
		list.clear();
		while(cursor.moveToNext()) {
			zfbInfo = new ZfbInfo();
			zfbInfo.id = cursor.getInt(cursor.getColumnIndex("id"));
			zfbInfo.riqi = cursor.getString(cursor.getColumnIndex("riqi"));
			zfbInfo.benjin = cursor.getString(cursor.getColumnIndex("benjin"));
			zfbInfo.jinglirun = cursor.getString(cursor.getColumnIndex("jinglirun"));
			zfbInfo.shengyutianshu = cursor.getString(cursor.getColumnIndex("shengyutianshu"));
			zfbInfo.yuqililv = cursor.getString(cursor.getColumnIndex("yuqililv"));
			zfbInfo.bianxianlilv = cursor.getString(cursor.getColumnIndex("bianxianlilv"));
			zfbInfo.kebianxian = cursor.getString(cursor.getColumnIndex("kebianxian"));
			zfbInfo.zongtianshu = cursor.getString(cursor.getColumnIndex("zongtianshu"));
			zfbInfo.gudinglilv = cursor.getString(cursor.getColumnIndex("gudinglilv"));
			zfbInfo.bianxianshouxufei = cursor.getString(cursor.getColumnIndex("bianxianshouxufei"));
			zfbInfo.yingfulixi = cursor.getString(cursor.getColumnIndex("yingfulixi"));
			zfbInfo.shijidaozhangjine = cursor.getString(cursor.getColumnIndex("shijidaozhangjine"));
			zfbInfo.bianxianhoushouyi = cursor.getString(cursor.getColumnIndex("bianxianhoushouyi"));
			zfbInfo.bubianxianshouyi = cursor.getString(cursor.getColumnIndex("bubianxianshouyi"));
			list.add(zfbInfo);
		}
		cursor.close();
		db.close();
	}
}
